#ifndef ACTUALDATAMODEL_H
#define ACTUALDATAMODEL_H
#include <QTreeWidgetItem>
#include <defines.h>
#include <QDebug>

class ActualDataModel
{
private:
    ActualDataModel();
public:
    QStringList taglist;

    static ActualDataModel& getInstance()
    {
        static ActualDataModel* inst = 0;
        if(inst==0)
            return *(inst = new ActualDataModel());
        else
            return *inst;
    }

    static QTreeWidgetItem* getRoot()
    {
        static QTreeWidgetItem* inst = 0;
        if(inst) return inst;
        else
        {
            inst = new QTreeWidgetItem();
            inst->setText(0,"All bookmarks");
            inst->setText(TYPE,FOLDERSTR);
            return inst;
        }
    }

    static
    QTreeWidgetItem* add(QTreeWidgetItem* node, QString name,QStringList tags,QString addr, QString thumbs,FILETYPE ftype)
    {
        QTreeWidgetItem* newchild = new QTreeWidgetItem(node);
        newchild->setText(NAME,name);
        newchild->setText(TAG,tags.join(", "));
        newchild->setData(TAG,Qt::UserRole,QVariant(tags));
        newchild->setText(ADDR,addr);
        newchild->setText(THUMB,thumbs);
        newchild->setText(TYPE, TOTYPESTR(ftype));
        node->addChild(newchild);
        for(int i=0; i<tags.count(); i++)
        {
            QString thisTag = tags[i];
            QStringList& taglist = ActualDataModel::getInstance().taglist;
            if(!taglist.contains(thisTag) && !thisTag.isEmpty())
            {
                taglist.append(thisTag);
            }
        }

        return newchild;
    }

    static
    void remove(QTreeWidgetItem* node)
    {
        //remove tags if necessary
        QStringList tags = node->data(TAG,Qt::UserRole).toStringList();
        for(int i=0; i<tags.count(); i++)
        {
            QList<QTreeWidgetItem*> list = inOrderTraversal(tags[i],TAG,ActualDataModel::contains);
            if(list.count()==1) //only this item uses this tag
            {
                ActualDataModel::getInstance().taglist.removeAll(tags[i]);
            }
        }
        if(node->parent())
        {
            QTreeWidgetItem* p = node->parent();
            p->removeChild(node);
        }
    }

    static
    void edit(QTreeWidgetItem* node, QString name,QStringList tags,QString addr,QString tpath)
    {
        //add/remove tags if necessary
        for(int i=0; i<tags.count(); i++)
        {
            QList<QTreeWidgetItem*> list = inOrderTraversal(tags[i],TAG,ActualDataModel::contains);
            if(list.count()==0) //tag doesn't exist yet
            {
                ActualDataModel::getInstance().taglist.append(tags[i]);
            }
        }
        QStringList currentTags = node->text(TAG).split(", ");
        for(int i=0; i<currentTags.count(); i++)
        {
            QList<QTreeWidgetItem*> list = inOrderTraversal(currentTags[i],TAG,ActualDataModel::contains);
            if(list.count()==1 && !tags.contains(currentTags[i])) //only this item uses this tag
            {
                ActualDataModel::getInstance().taglist.removeAll(currentTags[i]);
            }
        }
        //set the node text
        node->setText(NAME,name);
        node->setText(TAG,tags.join(", "));
        node->setData(TAG,Qt::UserRole,QVariant(tags));
        node->setText(ADDR,addr);
        node->setText(THUMB,tpath);
    }

    static bool equals(QString& a, QString& b)
    {
        return a == b;
    }

    static bool contains(QString& a, QString& b)
    {
        return a.contains(b,Qt::CaseInsensitive);
    }

    static QTreeWidgetItem* search(QString& _data, COLSDATA type)
    {
        return recSearch(getRoot(),_data,type);
    }

    static QTreeWidgetItem*
    recSearch(QTreeWidgetItem* node,QString& _data, COLSDATA type);

    static QList<QTreeWidgetItem*> inOrderTraversal(QString& _data, COLSDATA type, bool (*func)(QString&, QString&))
    {
        QList<QTreeWidgetItem*> results;
        inOrderTraversalRec(getRoot(),_data,type,func,&results);
        for(int i=0;i<results.size();i++)
            qDebug()<<results.at(i)->text(0);
        return results;
    }

    static void inOrderTraversalRec(QTreeWidgetItem* node,QString& _data, COLSDATA type, bool (*func)(QString&, QString&), QList<QTreeWidgetItem*>* list);
};

#endif // ACTUALDATAMODEL_H
